//
//  ByHomeLotteryCell.m
//  caipiao_ios
//
//  Created by mc on 2018/7/6.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByHomeLotteryCell.h"

@implementation ByHomeLotteryCell

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByHomeLotteryCell";
    ByHomeLotteryCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self initViews];
    }
    return self;
}


- (void)initViews{
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    self.contentView.backgroundColor = BY_BACKGROUND_COLOR;
    UIView *backgroundView = [[UIView alloc] init];
    [self.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(8);
        make.right.equalTo(self.contentView).offset(-8);
        make.top.equalTo(self.contentView).offset(0);
        make.bottom.equalTo(self.contentView).offset(-9);
    }];
    backgroundView.layer.cornerRadius = 5;
    backgroundView.layer.masksToBounds = YES;
    backgroundView.backgroundColor = [UIColor whiteColor];
    
    UILabel *typeLabel = [[UILabel alloc] init];
    [backgroundView addSubview:typeLabel];
    [typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(8);
        make.top.equalTo(backgroundView).offset(13);
        make.size.mas_equalTo(CGSizeMake(50, 15));
    }];
    typeLabel.font = FONT(14);
    typeLabel.textColor = BY_THEME_COLOR;
    typeLabel.text = @"双色球";
    
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundView addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-6);
        make.centerY.equalTo(typeLabel.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(40, 20));
    }];
    [moreBtn setTitle:@"更多>" forState:UIControlStateNormal];
    [moreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    moreBtn.titleLabel.font = FONT(11);
    self.moreBtn = moreBtn;
    
    
    UILabel *jackpotLabel = [[UILabel alloc] init];
    [backgroundView addSubview:jackpotLabel];
    [jackpotLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(typeLabel.mas_right).offset(0);
        make.top.equalTo(typeLabel.mas_top).offset(0);
        make.right.equalTo(moreBtn.mas_left).offset(-5);
        make.height.mas_equalTo(@12);
    }];
    
    jackpotLabel.font = FONT(11);
    jackpotLabel.textColor = BY_THEME_COLOR;
    jackpotLabel.text = @"奖池 3亿3333万";
    self.jackpotLabel = jackpotLabel;

    UIButton *updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundView addSubview:updateBtn];
    [updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-7);
        make.centerY.equalTo(backgroundView).offset(0);
        make.size.mas_equalTo(CGSizeMake(25, 25));
    }];
    [updateBtn setImage:[UIImage imageNamed:@"shuaxin"] forState:UIControlStateNormal];
    self.updateBtn = updateBtn;
    
    UIButton *bettingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backgroundView addSubview:bettingBtn];
    [bettingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backgroundView).offset(-5);
        make.bottom.equalTo(backgroundView).offset(-7);
        make.size.mas_equalTo(CGSizeMake(80, 25));
    }];
    [bettingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bettingBtn setTitle:@"投注2元" forState:UIControlStateNormal];
    bettingBtn.backgroundColor = BY_THEME_COLOR;
    bettingBtn.titleLabel.font = FONT(12);
    bettingBtn.layer.cornerRadius = 5;
    self.bettingBtn = bettingBtn;
    
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [backgroundView addSubview:timeLabel];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(10);
        make.centerY.equalTo(bettingBtn.mas_centerY).offset(-5);
        make.right.equalTo(bettingBtn.mas_left).offset(-5);
        make.height.mas_equalTo(@15);
    }];
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.font = FONT(11);
    timeLabel.text = @"第10000期 01/17 17：00开奖";
    self.timeLabel = timeLabel;
    
    [updateBtn addTarget:self action:@selector(updateAction:) forControlEvents:UIControlEventTouchUpInside];
    [moreBtn addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    [bettingBtn addTarget:self action:@selector(bettingAction:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat magin = 10;
    CGFloat width = (SCREEN_WIDTH - 45  - magin * 8 - 30) / 10.0;
    ByHomeCirclesVIew *circlesView = [[ByHomeCirclesVIew alloc] init];
    [backgroundView addSubview:circlesView];
    [circlesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backgroundView).offset(0);
        make.centerY.equalTo(backgroundView).offset(0);
        make.right.equalTo(updateBtn.mas_left).offset(-20);
        make.height.mas_offset(@(width + 20));
    }];
    self.circlesView = circlesView;
    self.typeLabel = typeLabel;
    
    UIImageView *stateView = [[UIImageView alloc] init];
    [self addSubview:stateView];
    stateView.image = [UIImage imageNamed:@"已停售状态"];
    [stateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backgroundView).offset(0);
        make.right.equalTo(backgroundView).offset(0);
        make.size.mas_equalTo(CGSizeMake(69, 63));
    }];
    stateView.hidden = YES;
    self.stateView = stateView;
}

- (void)setNetModel:(ByNetModel *)netModel{
    _netModel = netModel;
    self.timeLabel.text = [NSString stringWithFormat:@"第%@期 %@开奖",_netModel.number,_netModel.opentime];
    self.jackpotLabel.text = [NSString stringWithFormat:@"奖池 %@", _netModel.next_all_money];
}

- (void)setIsStopSale:(BOOL)isStopSale{
    _isStopSale = isStopSale;
    if (_isStopSale) {
        self.stateView.hidden = NO;
        self.typeLabel.textColor = [UIColor lightGrayColor];
        self.jackpotLabel.textColor = [UIColor lightGrayColor];
        self.bettingBtn.backgroundColor = [UIColor lightGrayColor];
        self.bettingBtn.userInteractionEnabled = NO;
        self.updateBtn.userInteractionEnabled = NO;
        self.moreBtn.userInteractionEnabled = NO;
    }else{
        self.stateView.hidden = YES;
        self.typeLabel.textColor = BY_THEME_COLOR;
        self.jackpotLabel.textColor = BY_THEME_COLOR;
        self.bettingBtn.backgroundColor = BY_THEME_COLOR;
        self.bettingBtn.userInteractionEnabled = YES;
        self.updateBtn.userInteractionEnabled = YES;
        self.moreBtn.userInteractionEnabled = YES;
    }
}

- (void)moreAction:(UIButton *)btn{
    if (self.moreBlock) {
        self.moreBlock();
    }
}

- (void)updateAction:(UIButton *)btn{
    if (self.updateBlock) {
        self.updateBlock();
    }
    
}

- (void)bettingAction:(UIButton *)btn{
    if (self.bettingBlock) {
        self.bettingBlock();
    }
}

@end
